package vista;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import comun.Util;
import controlador.Sistema;
import modelo.Prenda;


@SuppressWarnings("serial")
public class ListaPrendasPanel extends JPanel {

	private JButton modificarB, volverB;
	private JScrollPane prendasSP;
	private JTable prendasT;
	private ModificarPrendasTableModel tableModel;
	private String tipoProducto;
	private List<? extends Prenda> prendas;
	public ListaPrendasPanel(List<? extends Prenda> prendas, String tipoProducto) {
		this.tableModel = new ModificarPrendasTableModel(prendas);
		this.prendas = prendas;
		this.tipoProducto = tipoProducto;
		initGui();
		layoutComponents();
		initActions();
	}

	private void initGui() {
		volverB = new JButton("Volver");
		modificarB = new JButton("Modificar");

		prendasT = new JTable(tableModel);
		prendasT.setPreferredScrollableViewportSize(new Dimension(300,200));
		prendasSP = new JScrollPane(prendasT);
	}

	

	private void layoutComponents() {
		this.setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 5;
		c.ipadx = 180;
		c.ipady = 100;
		c.fill = GridBagConstraints.BOTH;
		add(prendasSP, c);
		
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.ipady = 0;
		c.insets = new Insets(20, 0, 20, 0);

		
		add(modificarB, c);
		c.gridx = 2;
		add(volverB, c);

	}

	private void initActions() {
		modificarB.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Prenda prenda = getPrendaSeleccionada();
				if(prenda != null) {
					Sistema.getInstance().mostrarPrenda(prenda.getCodigo());
					clear();
					SwingUtilities.windowForComponent(ListaPrendasPanel.this).dispose();
				} else {
					notificarError("Por favor seleccione una prenda del listado");
				}
			}
		});

		volverB.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clear();
				SwingUtilities.windowForComponent(ListaPrendasPanel.this).dispose();
			}
		});
		
	}
	
	private Prenda getPrendaSeleccionada() {
		return tableModel.getPrendaSeleccionada();
	}
	
	private void clear() {
		for(Prenda prenda: prendas) {
			prenda.setSelected(false);
			prenda.setCantidad(0);
		}
		
	}
	
	public void showPanel() {
		final JFrame mainFrame = new JFrame();
		mainFrame.setTitle(tipoProducto);
		mainFrame.getContentPane().add(this);
		mainFrame.setPreferredSize(new Dimension(600, 450));
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		mainFrame.pack();
		mainFrame.setLocationRelativeTo(null);
		mainFrame.setVisible(true);
	}
	
	private void notificarError(String error) {
		Util.displayErrorMessage(ListaPrendasPanel.this,error);
	}
	
	public void actualizarPrendas(List<Prenda> prendas) {
		tableModel.setPrendas(prendas);
	}

}